

package com.springcloud.sc.ucpv5.client.commondata;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

import com.springcloud.sc.ucpv5.client.commondata.data.Node;
import com.springcloud.sc.ucpv5.client.commondata.tree.IDcsTreeNode;
import com.springcloud.sc.ucpv5.client.contentmain.MainConstant;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;

/**
 * Comments : 6-17
 * 
 */

public class BaseListModel {
    private IDcsTreeNode stub;

    public IDcsTreeNode getStub() {
	return stub;
    }

    public BaseListModel(IDcsTreeNode stub) {
	super();
	this.stub = stub;
	totalUserResult.add(0);
	totalUserResult.add(0);
    }

    private CopyOnWriteArrayList<MyNode> nodeList = new CopyOnWriteArrayList<MyNode>();
    private CopyOnWriteArrayList<MyGroup> groupList = new CopyOnWriteArrayList<MyGroup>();
    private CopyOnWriteArrayList<MyUser> userList = new CopyOnWriteArrayList<MyUser>();

    public List<MyNode> getNodeList() {
	return nodeList;
    }

    public boolean isSameNode(MyNode nodeStub, Node node) {
	try {

	    if (nodeStub.getIdlData().Physics_NodeNumber.equals(node.Physics_NodeNumber) && nodeStub.getIdlData().NodeNumber.equals(node.NodeNumber)) {
		return true;
	    }
	} catch (Exception e) {

	}

	return false;
    }

    public List<MyGroup> getGroupList() {
	return groupList;
    }

    public void addGroup(ArrayList<MyGroup> list) {
	for (MyGroup node : list) {
	    if (!groupList.contains(node)) {
		groupList.add(node);
		node.setParent(stub);
	    }
	}
    }

    public void clear() {
	userList.clear();
	groupList.clear();
	nodeList.clear();
	
	// wsy 20181210 需要考虑是否需要从BaseDataManager 中将整体缓存的组和用户都删掉...
	// 如果不删,有可能整体中存在的组或用户, 但是节点中不存在了.
    }

    public void addNode(MyNode node, boolean notifyChg) {
	if (!nodeList.contains(node)) {
	    nodeList.add(node);
	    node.setParent(stub);
	    node.setLevel(stub.getLevel() + 1);
	}
    }

    public boolean removeNode(MyNode node, boolean notifyChg) {
	boolean ret = nodeList.remove(node);
	if (ret) {
	    BaseDataManager.getInstance().nodeRmv(node);

	}

	return ret;
    }

    public void addGroup(MyGroup node, boolean notifyChg) {
	if (!groupList.contains(node)) {
	    groupList.add(node);
	    node.setParent(stub);

	}
    }

    public boolean removeGroup(ArrayList<MyGroup> list, boolean notifyChg) {
	boolean ret = false;
	for (MyGroup node : list) {
	    if (groupList.remove(node)) {
		node.rmvParent(stub);
		ret = true;
	    }
	}


	return ret;
    }

    public boolean removeGroup(MyGroup node, boolean notifyChg) {
	boolean ret = groupList.remove(node);
	if (ret) {
	    node.rmvParent(stub);
	}

	return ret;
    }

    public void addUser(ArrayList<MyUser> list, boolean notifyChg) {
	boolean ret = false;
	try {
	    for (MyUser node : list) {
		addUser(node, false);
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public void addUser(MyUser node) {
	addUser(node, true);
    }

    public synchronized boolean addUser(MyUser node, boolean notifyEvent) {
	if (node == null)
	    return false;
	try {
	    if (!userList.contains(node)) {
		userList.add(node);
		node.setParent(stub);
		return true;
	    }
		return true;
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return false;
    }

    public boolean removeUser(MyUser node, boolean notifyEvent) {
	if (node == null)
	    return true;
	try {
	    userList.remove(node);

	    return true;
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return false;
    }

    public boolean removeUser(List<MyUser> nodeList) {
	if (nodeList == null)
	    return true;
	boolean ret = false;
	try {
	    for (MyUser node : nodeList) {
		if (ret) {
		    userList.remove(node);
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return ret;
    }

    // 分配增加用户
    public synchronized void pushUser(MyUser node) {
	if (node == null)
	    return;
	try {
	    if (!userList.contains(node)) {
		userList.add(node);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    // 分配删除用户
    public boolean popUser(MyUser node) {
	if (node == null)
	    return true;
	try {
	    userList.remove(node);

	    return true;
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return false;
    }

    public ArrayList<MyUser> getUserList() {
	// 取得用户列表时,判断该用户是否属于组或节点
	ArrayList<MyUser> userList_ = new ArrayList<MyUser>();
	try {
	    String grpNum = null;
	    if (this.stub instanceof MyGroup) {
		grpNum = ((MyGroup) this.stub).getFieldText(MainConstant.FIELD_TYPE_TEL);
	    } else if (this.stub instanceof MyNode) {
		grpNum = null;
	    }
	    for (MyUser user : userList) {
		if (user.isMediaFatherUser()) {
		    // 如果隐藏掉,会再本地存储时,不能存储.
		    // continue;
		}

		if (user.getUserTelNo().startsWith("F")) {
		    continue;
		}

		String[] grpList = user.getIdlData().GrpList.split("#");
		if (grpNum == null) {
		    if (grpList.length == 0) {
			;
		    }
		    if (grpList.length == 1 && grpList[0].equals("")) {
			// 20150519 集成管理的用户不在节点下显示
			{
			    userList_.add(user);
			}
		    }
		} else {
		    for (String grp : grpList) {
			if (grp.equals(grpNum)) {
			    userList_.add(user);
			    break;
			}
		    }
		}
	    }
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	}

		if (this.stub instanceof MyGroup) {
				// 根据组号码缓存的用户排序进行排序,然后返回
			if(((MyGroup)stub).getGroupDataInfo() != null && ((MyGroup)stub).getGroupDataInfo().groupUserLevel != null){
				if(!((MyGroup)stub).getGroupDataInfo().groupUserLevel.isEmpty()){
					((MyGroup)stub).getGroupDataInfo().sortGroupUser(userList_);
				}
			}
		}
	return userList_;
    }

    public int getTotalSize() {
	return userList.size();
    }

    public int getTotalOnlineSize() {
	int userOnline = 0;
	for (MyUser user : userList) {
	    if (user != null && user.isOnline()) {
		userOnline++;
	    }
	}
	return userOnline;
    }

    private List<Integer> totalUserResult = new ArrayList<Integer>();

    public List<Integer> TotalUserResult() {
	return totalUserResult;
    }

    public List<Integer> TotalUserGet() {
	int userTotal = 0;
	int userOnLineTotal = 0;
	List<Integer> returnValue = new ArrayList<Integer>();
	CopyOnWriteArraySet<MyUser> userSetTotal = new CopyOnWriteArraySet<MyUser>();
	CopyOnWriteArraySet<MyUser> onlineUserSetTotal = new CopyOnWriteArraySet<MyUser>();

	returnValue.add(userTotal);
	returnValue.add(userOnLineTotal);

	totalUserResult = TotalUserGet(userSetTotal, onlineUserSetTotal, returnValue);

	return returnValue;
    }

    public List<Integer> TotalUserGet(Set<MyUser> userSetTotal, CopyOnWriteArraySet<MyUser> onlineUserSetTotal, List<Integer> returnValue) {
	int userTotal = returnValue.get(0);
	int userOnLineTotal = returnValue.get(1);

	for (MyNode node : nodeList) {
	    returnValue = node.getListModel().TotalUserGet(userSetTotal, onlineUserSetTotal, returnValue);
	    userTotal = returnValue.get(0);
	    userOnLineTotal = returnValue.get(1);
	}

	for (MyGroup group : groupList) {
	    if (group.isTmpGroup()) {
		continue;
	    }
	    if (group.isLoad()) {
		ArrayList<MyUser> userList = group.getListModel().getUserList();
		userTotal += userList.size();
		for (MyUser user : userList) {
		    if (userSetTotal.contains(user)) {
			userTotal--;
		    } else {
			userSetTotal.add(user);
		    }

		    if (user.isOnline()) {
			userOnLineTotal++;
			if (onlineUserSetTotal.contains(user)) {
			    userOnLineTotal--;
			} else {
			    onlineUserSetTotal.add(user);
			}
		    }
		}
	    } else {
		userTotal += group.getListModel().getUserOwnTotal();
		userOnLineTotal += group.getListModel().getUserOwnOnLineTotal();
	    }

	    // 涉及下级组的处理
	    returnValue.clear();
	    returnValue.add(userTotal);
	    returnValue.add(userOnLineTotal);

	    returnValue = group.getListModel().TotalUserGet(userSetTotal, onlineUserSetTotal, returnValue);

	    userTotal = returnValue.get(0);
	    userOnLineTotal = returnValue.get(1);
	}

	if (stub instanceof MyGroup) {
	    MyGroup group = (MyGroup) stub;
	    if (group.isLoad()) {
		for (MyUser user : userList) {
		    if (userSetTotal.contains(user)) {
			userTotal--;
		    } else {
			userSetTotal.add(user);
		    }
		    if (user.isOnline()) {
			userOnLineTotal++;
			if (onlineUserSetTotal.contains(user)) {
			    userOnLineTotal--;
			} else {
			    onlineUserSetTotal.add(user);
			}
		    }
		}
	    } else {
		userTotal += group.getListModel().getUserOwnTotal();
		userOnLineTotal += group.getListModel().getUserOwnOnLineTotal();
	    }
	} else if (stub instanceof MyNode) {
	    for (MyUser user : userList) {
		if (userSetTotal.contains(user)) {
		    userTotal--;
		} else {
		    userSetTotal.add(user);
		}
		if (user.isOnline()) {
		    userOnLineTotal++;
		    if (onlineUserSetTotal.contains(user)) {
			userOnLineTotal--;
		    } else {
			onlineUserSetTotal.add(user);
		    }
		}
	    }
	}

	return returnValue;
    }

    // 记录在线成员数和用户总数
    private int userOnLine = 0;
    private int userAllSize = 0;

    public boolean setUserOnLine(int userOnLie_) {
	if (this.userOnLine == userOnLie_) {
	    // 没有改变,不处理
	    return false;
	} else {
	    this.userOnLine = userOnLie_;
	}
	// 产生改变
	return true;
    }

    public boolean setUserAllSize(int userAllSize_) {
	if (this.userAllSize == userAllSize_) {
	    // 没有改变,不处理
	    return false;
	} else {
	    this.userAllSize = userAllSize_;
	}
	// 产生改变
	return true;
    }

    public int getUserOwnOnLineTotal() {
	return userOnLine;
    }

    public int getUserOwnTotal() {
	return userAllSize;
    }

    public void updateUserOwnOnLine() {
	int userOnline_ = 0;
	for (MyUser user : getUserList()) {
	    if (user != null && user.isOnline()) {
		userOnline_++;
	    }
	}
	userOnLine = userOnline_;
    }

    // 出现组内用户状态变更时,更新在线用户数据
    public void updateUserOwnTotal() {
	userAllSize = getUserList().size();
    }

    // 是否存在下级组, 临时组不算入内
    public boolean isSubGroupExist() {
	boolean is = false;
	for (MyGroup group : groupList) {
	    if (group.isTmpGroup()) {
		continue;
	    }
	    is = true;
	    ;
	    break;
	}
	return is;
    }
}
